package com.test.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2017/4/28
 * Time: 11:10
 * To change this template use Editor | File and Code Templates.
 */
public class AddThread extends Thread {

    /**
     * volatile 关键字只是可见性，并不能保证其原子性
     * synchronized 则包含可见性、原子性
     * 如果不加，则会读取线程本地存储的值，相反则强制读取共享内存区的值（方法区）
     */
    private static volatile boolean isRunning = true;

    public void setRunning(boolean running) {
        isRunning = running;
    }


    @Override
    public void run() {
        System.out.println("进入run方法中了");
        while (isRunning) {

        }
        System.out.println("线程执行完了");
    }

}

class runtest {
    public static void main(String[] args) {
        try {
//            AddThread thread = new AddThread();
//            thread.start();
//            Thread.sleep(2000);
//            thread.setRunning(false);
            MathAddTest addTest = new MathAddTest(0);
            ExecutorService service = Executors.newFixedThreadPool(10);
            for (int j = 0; j < 50; j++) {
//                service.execute(() -> addTest.addNum());
                new Thread(() -> {
                    addTest.addNum();
                }).start();
            }
            service.shutdown();
            Thread.sleep(1000);
            System.out.println("num = " + addTest.getNum());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
